package cn.itcast.erp.service.impl;

import cn.itcast.erp.dao.mapper.GoodsMapper;
import cn.itcast.erp.dao.mapper.GoodstypeMapper;
import cn.itcast.erp.entity.Goods;
import cn.itcast.erp.entity.Goodstype;
import cn.itcast.erp.entity.utils.AjaxResult;
import cn.itcast.erp.service.GoodsService;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

@Service("goodsService")
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    private GoodsMapper goodsMapper;

    @Autowired
    private GoodstypeMapper goodstypeMapper;

    @Override
    public int deleteGoodsByUuId(Long uuid) {

        if (uuid != null) {
            System.out.println("111" + uuid);
            int i = this.goodsMapper.deleteGoodsByUuId(uuid);
            return i;
        }
        return -1;

    }

    @Override
    public int insertGoods(Goods goods) {
        if (goods != null) {
            int i = this.goodsMapper.insertGoods(goods);
            return i;
        }
        return -1;

    }

    @Override
    public int updateGoods(Goods goods) {
        if (goods != null) {
            int i = this.goodsMapper.updateGoods(goods);
            return i;
        }
        return -1;
    }

    @Override
    public AjaxResult findGoods(Goods goods) {

        int total = this.goodsMapper.count(goods);
        List <Goods> rows = this.goodsMapper.findGoods(goods);
        AjaxResult ar = new AjaxResult();
        ar.setTotal(total);
        ar.setRows(rows);
        return ar;
    }

    @Override
    public List <Goods> findAllGoods() {
        return this.goodsMapper.findAllGoods();
    }

    @Override
    public Goods findGoodsById(Long uuid) {
        return this.goodsMapper.findGoodsById(uuid);
    }


    //导出Excel
    @Override
    public InputStream getAllGoodsEx() {
        //添加数据
        List <Goods> goods = goodsMapper.getAllGoodsEx();

        HSSFWorkbook sheets = new HSSFWorkbook();  // 定义一个新的工作簿
        Sheet sheet = sheets.createSheet("商品信息");  // 创建第一个新的Sheet页


        //创建第一行（也可以成为表头）
        Row row1 = sheet.createRow(0);
        sheet.setColumnWidth(0, 20 * 256);
        sheet.setColumnWidth(1, 30 * 256);
        sheet.setColumnWidth(2, 20 * 256);
        sheet.setColumnWidth(3, 30 * 256);
        sheet.setColumnWidth(4, 20 * 256);
        sheet.setColumnWidth(5, 30 * 256);
        sheet.setColumnWidth(6, 20 * 256);
        sheet.setColumnWidth(7, 30 * 256);

        //给表头第一行一次创建单元格
        String[] headers = {"编号", "名称", "产地", "厂家", "计量单位", "进货价格", "销售价格", "商品类型"};
        for (int i = 0; i < headers.length; i++) {
            row1.createCell(i).setCellValue(headers[i]);
            row1.setHeightInPoints(30);//设置行的高度是30
        }

        //向单元格里添加数据
        for (short i = 0; i < goods.size(); i++) {
            row1 = sheet.createRow(i + 1);
            row1.createCell(0).setCellValue(goods.get(i).getUuid());
            row1.createCell(1).setCellValue(goods.get(i).getName());
            row1.createCell(2).setCellValue(goods.get(i).getOrigin());
            row1.createCell(3).setCellValue(goods.get(i).getProducer());
            row1.createCell(4).setCellValue(goods.get(i).getUnit());
            row1.createCell(5).setCellValue(goods.get(i).getInprice());
            row1.createCell(6).setCellValue(goods.get(i).getOutprice());
            //通过商品类型的uuid来查询商品类型名字再返回给商品
            Goodstype byuuid = goodstypeMapper.findByuuid(goods.get(i).getGoodstypeuuid());
            row1.createCell(7).setCellValue(byuuid.getName());
            row1.setHeightInPoints(30);//设置行的高度是30
        }
        //字节流缓存区
        ByteArrayOutputStream bo = new ByteArrayOutputStream();
        try {
            sheets.write(bo);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new ByteArrayInputStream(bo.toByteArray());
    }


}

